package com.rongshu.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.rongshu.R;

public class VersionUpdateDialog extends Dialog {

    public VersionUpdateDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Context context; // 上下文对象
        //layout
        private int mMainLayout = R.layout.dialog_version_update;
        //view
        private TextView txt_version_update_content, txt_left_hint, txt_right_update;
        //listener
        private OnClickListener listener;
        //更新内容
        private String versionUpdateContent;
        //左边文字
        private String leftHint = "";
        //右边提示
        private String rightHint = "";

        public Builder(Context context) {
            this.context = context;
        }

        public void setVersionUpdateContent(String versionUpdateContent) {
            this.versionUpdateContent = versionUpdateContent;
        }

        public Builder setLeftHint(String leftHint) {
            this.leftHint = leftHint;
            return this;
        }

        public Builder setRightHint(String rightHint) {
            this.rightHint = rightHint;
            return this;
        }

        public Builder setListener(OnClickListener listener) {
            this.listener = listener;
            return this;
        }

        public VersionUpdateDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final VersionUpdateDialog dialog = new VersionUpdateDialog(context, R.style.CustomDialog);
            View layout = inflater.inflate(mMainLayout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            txt_version_update_content = layout.findViewById(R.id.txt_version_update_content);
            txt_left_hint = layout.findViewById(R.id.txt_left_hint);
            txt_right_update = layout.findViewById(R.id.txt_right_update);
            txt_version_update_content.setText(versionUpdateContent);
            txt_version_update_content.setVisibility(TextUtils.isEmpty(versionUpdateContent) ? View.GONE : View.VISIBLE);
            txt_left_hint.setText(leftHint);
            txt_left_hint.setVisibility(TextUtils.isEmpty(leftHint) ? View.GONE : View.VISIBLE);
            txt_right_update.setText(rightHint);
            txt_left_hint.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.leftClickListener();
                    }
                }
            });
            txt_right_update.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.rightClickListener();
                    }
                }
            });
            return dialog;
        }

        public interface OnClickListener {
            void leftClickListener();

            void rightClickListener();
        }
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams params = this.getWindow().getAttributes();
        params.width = (int) getContext().getResources().getDimension(R.dimen.dd_dimen_562px);
        this.getWindow().setAttributes(params);
    }
}
